﻿using System;
using System.Collections.Generic;
using Shell.PocoDot;

namespace Shell.DomainDot
{
    public class DefaultDomainDotFactory : IDomainDotFactory
    {
        public virtual TDomainDot Create<TDomainDot>(Guid? key)
            where TDomainDot : DomainDotBase
        {
            var dot = this.CreateDomainDot<TDomainDot>();
            dot.Context = this.CreateContext(key);
            this.Inject(dot);
            dot.Initialize();
            return dot;
        }

        protected virtual DomainDotContext CreateContext(Guid? key)
        {
            return new DomainDotContext
            {
                Key = key,
            };
        }

        protected virtual TDomainDot CreateDomainDot<TDomainDot>()
            where TDomainDot : DomainDotBase
        {
            return ObjectSeeker.Seeker.Seek<TDomainDot>();
        }

        protected virtual void Inject<TDomainDot>(TDomainDot dot)
            where TDomainDot : DomainDotBase
        {
            foreach (var injector in DomainInjectors.Injectors)
            {
                injector.Inject(dot);
            }
        }
    }
}
